// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0

package com.aws.rest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import java.util.stream.Collectors;
import java.util.stream.StreamSupport;

@ComponentScan(basePackages = { "com.aws.services" })
@CrossOrigin(origins = "*")
@RestController
@RequestMapping("api/items")
public class MainController {
    private final WorkItemRepository repository;

    @Autowired
    MainController(
            WorkItemRepository repository) {
        this.repository = repository;
    }

    @GetMapping("")
    public List<WorkItem> getItems(@RequestParam(required = false) String archived) {
        Iterable<WorkItem> result;
        if (archived != null)
            result = repository.findAllWithStatus(archived);
        else
            result = repository.findAllWithStatus("");

        return StreamSupport.stream(result.spliterator(), false)
                .collect(Collectors.toUnmodifiableList());
    }

    // Notice the : character which is used for custom methods. More information can
    // be found here:
    // https://cloud.google.com/apis/design/custom_methods
    @PutMapping("{id}:archive")
    public List<WorkItem> modUser(@PathVariable String id) {
        repository.flipItemArchive(id);
        Iterable<WorkItem> result = repository.findAllWithStatus("false");
        return StreamSupport.stream(result.spliterator(), false)
                .collect(Collectors.toUnmodifiableList());
    }

    @PostMapping("")
    public List<WorkItem> addItem(@RequestBody Map<String, String> payload) {
        String name = payload.get("name");
        String guide = payload.get("guide");
        String description = payload.get("description");
        String status = payload.get("description");

        WorkItem item = new WorkItem();
        String workId = UUID.randomUUID().toString();
        String date = LocalDateTime.now().toString();
        item.setId(workId);
        item.setGuide(guide);
        item.setDescription(description);
        item.setName(name);
        item.setDate(date);
        item.setStatus(status);
        repository.save(item);

        // Return active records.
        Iterable<WorkItem> result = repository.findAllWithStatus("false");
        return StreamSupport.stream(result.spliterator(), false)
                .collect(Collectors.toUnmodifiableList());
    }
}